<template>
    <div id="release">
        <el-row>
            <el-col :span="18"
                    :offset="2">
                <div class="grid-content bg-white">
                    <div class="top_box bg-blue">
                        <div class="title_box">
                            <span class="title_style">Haring knowledge</span>
                        </div>
                        <div class="guide_box">
                            <span class="guide_style">发布知识库</span>
                        </div>
                        <div class="image_box">
                            <img src="~@/assets/release/image1.png" />
                        </div>
                    </div>
                    <div class="cell"></div>

                    <div class="operate_box">
                        <div class="time_box">
                            <span class="time_style fn13">是否同步到大厅：</span>
                            <span class="time_style1 fn13">任务大厅，展示给公司以外的用户接单</span>
                        </div>
                        <div class="time_radio">
                            <el-radio-group v-model="state.form.is_hall">
                                <el-radio  :label="1">是</el-radio>
                                <el-radio  :label="0">否</el-radio>
                            </el-radio-group>
                        </div>
                <div class="data_box">
                  <span class="data_style fn13">
                    附件
                  </span>
                </div>
                <div class="upload_box">
                    <upload-file ref="uploadFile" :fileList="state.fileList"></upload-file>
                    <el-dialog>
                        <img src="~@/assets/release/group.png" />
                    </el-dialog>
                </div>
                <div v-if="state.form.is_hall!=1">
                    <div class="title1_box">
                      <span class="title1_style fn13">
                        选择所属类型
                      </span>
                    </div>
                    <div class="group_box dfix">
                        <el-radio-group v-model="state.form.task_type" size="small">
                            <div class="redio-maright">
                                <el-radio-button label="1" border>技术部</el-radio-button>
                            </div>
                            <div class="redio-maright">
                                <el-radio-button label="2" border>项目部</el-radio-button>
                            </div>
                            <div class="redio-maright">
                                <el-radio-button label="3" border>新闻中心</el-radio-button>
                            </div>
                            <div class="redio-maright">
                                <el-radio-button label="4" border>设计组</el-radio-button>
                            </div>
                        </el-radio-group>
                    </div>
                </div>
                <div v-if="state.form.is_hall!=1">
                        <div class="title1_box">
                            <span class="title1_style fn13">单独设定人员【勾选者可见】</span>
                        </div>
                        <div class="radio_box">
                            <el-tree
                                    :data="state.selectData"
                                    show-checkbox
                                    node-key="id"
                                    :default-expanded-keys="state.defaultExpandedKeys"
                                    :default-checked-keys="state.defaultExpandedKeys"
                                    ref="tree"
                                    :collapse-tags="true"
                            >
                            </el-tree>
                        </div>
                </div>
                        <div class="cell1_box"></div>
                        <div class="submit_box" @click="addRelea">
                            <el-button class="submit_style">发布</el-button>
                        </div>
                    </div>

                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    import { getCurrentInstance, ref ,reactive, onMounted} from "vue";
    import { postKnowledge,getDepartment,releasDetail, addReleas } from "@/request/api";
    import uploadFile from '@/components/uploadFile/uploadFile'
    import {useRouter} from "vue-router";
    const state = reactive({
        checkboxGroup1: [],
        bmdata: [],
        defaultExpandedKeys: [],
        list_key: [],
        defaultCheckedKeys: [],
        selectData: [],
        fileList: [],
        form: {
            title : '',
            is_hall: 0,
            ktype: 1,
            user_arr: '',
            department: ''
        }
    })
    export default {
        components: {uploadFile},
        data: () => ({}),
        setup (props,{emit}) {
            const radio2 = ref("");
            const radio3 = ref("");
            const router = useRouter();
            state.id = router.currentRoute.value.query.id;
            const {ctx,proxy} = getCurrentInstance();
            //获取部门
            const init = () => {
                let param = {
                    pageSize: 100
                }
                getDepartment(param).then(res => {
                    state.bmdata = res.data.data
                    state.selectData   = res.data.list
                })
                //获取详情
                if(state.id > 0){
                    initDetail()
                }
            }

            //获取编辑的数据
            const initDetail = () => {
                let param = {
                    kb_id: state.id
                }
                //获取详情
                releasDetail(param).then(res => {
                    state.form = res.data
                    if(res.data.user_arr!='*') {
                        let str = res.data.user_arr.substr(0, res.data.user_arr.length - 1);
                        state.defaultExpandedKeys = str.split(',')
                    }

                    if(res.data.department!='' && typeof(res.data.department)!='undefined'){
                        let str1 = res.data.department.substr(0, res.data.department.length - 1);
                        state.list_key = str1.split(',')
                    }
                    if(res.data.about_file){
                        imageArrPush(res.data.about_file);
                    }

                })
            }
            /**
             * 组装 图片
             */
            const imageArrPush = (arr) => {
                if (arr.length === 0) {
                    return false
                }
                let data = []
                for (var i = 0;i < arr.length; i++){
                    data[i] = { name: arr[i].title, url: arr[i].url }
                }
                state.fileList = data
            }
            //发布
            const addRelea = () => {
                if(typeof(proxy.$refs.uploadFile.$refs.uploadRef.uploadFiles[0].response)!='undefined') {
                    state.form.about_file = proxy.$refs.uploadFile.$refs.uploadRef.uploadFiles[0].response.data.attc_id
                }
                if(!state.form.about_file){
                    proxy.$message.error('请上传文件')
                    return false
                }
                if(state.form.is_hall!=1) {
                    let rules = proxy.$refs.tree.getCheckedKeys();
                    if(rules.length > 0) {
                        state.form.user_arr = userArr(rules);
                    }
                }else{
                    state.form.user_arr = '*'
                }
                if(state.id > 0){
                    state.form.kb_id  = state.id
                    addReleas(state.form).then(res => {
                        if (res.ret == 200) {
                            proxy.$message.success('编辑知识库成功！')
                            setTimeout(function () {
                                router.push({name: 'knowledge-listKnowledge-20'});
                            }, 1000)
                        } else {
                            proxy.$message.error(res.msg)
                        }
                    })
                }else {
                    postKnowledge(state.form).then(res => {
                        if (res.ret == 200) {
                            proxy.$message.success('发布知识库成功！')
                            setTimeout(function () {
                                if(state.form.is_hall == 1){
                                    router.push({name: 'user-knowledge-16'});
                                }else{

                                    router.push({name: 'knowledge-listKnowledge-20'});
                                }

                            }, 1000)
                        } else {
                            proxy.$message.error(res.msg)
                        }
                    })
                }
            }
            const userArr = (data) => {
                let arr = []
                for (let i = 0;data.length > i;i++){
                    if(data[i] > 0){
                        arr.push(data[i]);
                    }
                }
                return arr.join(',');
            }
            const count = ref(1);
            onMounted(async () => {
                init();
            })
            return {
                title: ref(""),
                content: ref(""),
                radio1: ref('现金'),
                state,
                radio2,
                radio3,
                props,
                count,
                addRelea,
                userArr
            };
        }
    };
</script>

<style lang="scss" scoped>
    .grid-content {
        min-height: 1208px;
        border-radius: 25px;
    }
    .bg-white {
        background-color: #ffffff;
    }
    .top_box {
        height: 220px;
        border-top-left-radius: 25px;
        border-top-right-radius: 25px;
        position: relative;
    }
    .bg-blue {
        background-image: linear-gradient(#ffffff, #d0e3fe);
    }
    .title_box {
        position: absolute;
        top: 22px;
        left: 63px;
        height: 36px;
        .title_style {
            font-size: 34px;
            font-family: Microsoft YaHei;
            font-weight: bold;
            color: #4e72fc;
            line-height: 100px;
        }
    }
    .guide_box {
        position: absolute;
        top: 103px;
        left: 63px;
        height: 17px;
        .guide_style {
            font-size: 18px;
            font-family: Microsoft YaHei;
            color: #7a8499;
            font-weight: 400;
        }
    }
    .image_box {
        position: absolute;
        bottom: 0;
        right: 33px;
    }
    .cell {
        margin-top: 3px;
        height: 5px;
        background-color: #e2e9f7;
    }
    .operate_box {
        min-height: 980px;
        border-bottom-left-radius: 25px;
        border-bottom-right-radius: 25px;
        .title1_box {
            margin-left: 63px;
            margin-top: 30px;
            height: 17px;
            .title_style {
                font-size: 27px;
                font-family: PingFang SC;
                font-weight: bold;
                color: #2b2d33;
                line-height: 100px;
            }
        }
        .title_input {
            margin-left: 63px;
            margin-top: 20px;
            width: 80%;
        }
        .content_box {
            margin-top: 60px;
            margin-left: 63px;
        }
        .content_input {
            margin-top: 20px;
            margin-left: 63px;
            width: 80%;
        }
        .data_box {
            margin-top: 60px;
            margin-left: 63px;
        }
        .upload_box {
            margin-left: 63px;
            margin-top: 10px;
        }
        .group_box {
            margin-top: 30px;
            margin-left: 63px;
            display: flex;
            .group {
                width: 120px;
                height: 52px;
                display: flex;
                background-color: #f0f2f5;
                color: #858688;
                align-items: center;
                border-radius: 26px;
                padding-left: 42px;
                margin-right: 55px;
            }
            .group1 {
                background-color: #f4f1fd;
            }
            .group2 {
                background-color: #f4ebf6;
            }
            .group3 {
                background-color: #eef5fe;
            }
        }
        .type_box {
            margin-top: 30px;
            margin-left: 63px;
            .type_style {
            }
        }
        .type_select {
            margin-top: 20px;
            margin-left: 63px;
            display: flex;
            .demo_button {
                width: 23%;
                .demo_style {
                    height: 45px;
                    background-color: #f0f2f5;
                    color: #858688;
                    border-radius: 10px;
                }
                .demo_style1 {
                    background-color: #e8efff;
                    color: #6192ff;
                }
            }
        }
        .type_input {
            margin-top: 30px;
            margin-left: 60px;
            width: 55%;
        }
        .time_box {
            margin-top: 40px;
            margin-left: 63px;
            .time_style1 {
                color: red;
            }
        }
        .time_radio {
            margin-top: 10px;
            margin-left: 63px;
        }
        .time_input {
            margin-top: 30px;
            margin-left: 63px;
        }
        .department_box {
            width: 80%;
            margin-left: 15%;
            margin-top: 30px;
            margin-bottom: 60px;
            .top {
                display: flex;
                flex-wrap: wrap;
            }
            .bottom {
                margin-top: 20px;
                display: flex;
                flex-wrap: wrap;
            }
            .demo_button {
                width: 25%;
                .demo_style {
                    height: 45px;
                    background-color: #f0f2f5;
                    color: #858688;
                    border-radius: 10px;
                }
                .demo_style1 {
                    background-color: #e8efff;
                    color: #6192ff;
                }
            }
        }
    }

    .cell1_box {
        width: 95%;
        height: 1px;
        background-color: #e3e9f7;
        margin-left: 2.5%;
    }
    .submit_box {
        margin-left: 47%;
        margin-top: 40px;
        padding-bottom: 50px;
        .submit_style {
            width: 80px;
            height: 32px;
            background: #6c91ed;
            box-shadow: 0px 2px 6px 0px rgba(110, 132, 179, 0.2);
            border-radius: 8px;
            color: #ffffff;
        }
    }
    .dfix {
        display: flex;
    }
    .redio-maright {
        display: inline-block;
        margin-right: 30px;

    }
    .el-radio-button__inner {
        background: #d0e3fe !important;
    }
    .tree {
        margin-left: 63px;
        margin-top: 20px;
    }
    .img-se1 {
        padding-top: 25px;
    }
    .radio_box {
        margin-left: 55px;
        margin-top: 20px;
    }
    .fn13 {
        font-size: 13px;
    }
    .el-tree {
        position: relative;
        cursor: default;
        background: #FFFFFF;
        color: #606266;
        display: flex;
    }
</style>
